<?php

class m_mandor extends CI_Model{
	
	function _list($data){
		extract($data);
		$sql = "SELECT 
					mandor_nama,
					mandor_kode,
					mandor_status,
					mandor_kota,
					mandor_bu,
					mandor_alamat,
					mandor_npwp,
					mandor_telpon,
					mandor_fax,
					mandor_email,
					mandor_pengalaman,
					mandor_kemampuan,
					mandor_wakil,
					mandor_tukang1,
					mandor_tukang2,
					mandor_pekerja,
					m_keuangan_id,
					m_pendanaan_id,
					mandor_id
				FROM m_mandor
				WHERE 
					1 = 1 
		";
		
        
		if (@$search != ""){
            $sql .= " AND (0 = 1";
            $sql .= " OR mandor_nama like '%".@$search."%'";
            $sql .= " OR mandor_kode like '%".@$search."%'";
            $sql .= " OR mandor_status like '%".@$search."%'";
            $sql .= " OR mandor_kota like '%".@$search."%'";
            $sql .= " OR mandor_bu like '%".@$search."%'";
            $sql .= " OR mandor_alamat like '%".@$search."%'";
            $sql .= " OR mandor_telpon like '%".@$search."%'";
            $sql .= " OR mandor_email like '%".@$search."%'";
            $sql .= " OR mandor_pengalaman like '%".@$search."%'";
            $sql .= " OR mandor_kemampuan like '%".@$search."%'";
            $sql .= ")";
        }
        
		if (@$mandor_nama != "") $sql .= " AND mandor_nama like '%".@$mandor_nama."%'";
		if (@$mandor_kode != "") $sql .= " AND mandor_kode like '%".@$mandor_kode."%'";
		if (@$mandor_bu != "") $sql .= " AND mandor_bu like '%".@$mandor_bu."%'";
		if (@$mandor_alamat != "") $sql .= " AND mandor_alamat like '%".@$mandor_alamat."%'";
		if (@$mandor_npwp != "") $sql .= " AND mandor_npwp like '%".@$mandor_npwp."%'";
		if (@$mandor_telpon != "") $sql .= " AND mandor_telpon like '%".@$mandor_telpon."%'";
		if (@$mandor_fax != "") $sql .= " AND mandor_fax like '%".@$mandor_fax."%'";
		if (@$mandor_email != "") $sql .= " AND mandor_email like '%".@$mandor_email."%'";
		if (@$mandor_pengalaman != "") $sql .= " AND mandor_pengalaman like '%".@$mandor_pengalaman."%'";
		if (@$mandor_kemampuan != "") $sql .= " AND mandor_kemampuan like '%".@$mandor_kemampuan."%'";
		if (@$mandor_wakil != "") $sql .= " AND mandor_wakil like '%".@$mandor_wakil."%'";
		if (@$mandor_tukang1 != "") $sql .= " AND mandor_tukang1 like '%".@$mandor_tukang1."%'";
		if (@$mandor_tukang2 != "") $sql .= " AND mandor_tukang2 like '%".@$mandor_tukang2."%'";
		if (@$mandor_pekerja != "") $sql .= " AND mandor_pekerja like '%".@$mandor_pekerja."%'";
		if (@$m_keuangan_id != "") $sql .= " AND m_keuangan_id like '%".@$m_keuangan_id."%'";
		if (@$m_pendanaan_id != "") $sql .= " AND m_pendanaan_id like '%".@$m_pendanaan_id."%'";
		
		$query = $this->db->query($sql);
		$records = $query->num_rows();
		
		$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows)." , ".$rows;
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return '{ 
		  "total": "'.ceil($records / $rows).'", 
		  "page": "'.$page.'", 
		  "records": "'.$records .'",
		  "rows" : '.json_encode($result).'
		}';
	}
	
	function _insert($data){
		$this->db->insert('m_mandor',$data);
		return $this->db->insert_id();
	}
	
	function _update($data,$id){
		$this->db->where('mandor_id',$id);
		$this->db->update('m_mandor',$data);
		return 1;
	}
	
	function _delete($id){
		$this->db->where('mandor_id',$id);
		$this->db->delete('m_mandor');
		return 1;
	}
	
	function _deactivate($data,$id){
		$this->db->where('mandor_id',$id);
		$this->db->update('m_mandor',$data);
		return 1;
	}
}